#coding:utf-8
from django.utils import timezone
from django.db import models
from django.contrib.auth.models import Group
from lovewith.account.models import MtUser
from lovewith.account.views.modules.noticeapi import NoticeApi
from lovewith.api.dxw_sms_tools import DxwSmsApi
from lovewith.website.models import Province, City, Category


#商家服务分类
class ServiceCate(models.Model):
    account_type = (
        ('1', u'婚前9个月'),
        ('2', u'婚前6个月'),
        ('3', u'婚前3个月'),
        ('4', u'婚前1个月'),
        ('5', u'婚后1周'),
    )

    stage = models.CharField(choices=account_type, max_length=1, default=1, verbose_name=u'阶段')
    name = models.CharField(max_length=20, verbose_name=u'分类名')
    cate = models.ForeignKey(Category, verbose_name=u'所属类目')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = u'商家服务分类'
        db_table = 'mt_service_cate'


#服务商列表
class Supplier(models.Model):
    enable_type = (
        (-1, u'不通过'),
        (0, u'未审核'),
        (1, u'通过'),
    )

    company = models.CharField(max_length=255, verbose_name=u'商家名称')
    city = models.ForeignKey(City, verbose_name=u'城市')
    address = models.CharField(max_length=255, blank=True, null=True, verbose_name=u'实体地址')
    name = models.CharField(max_length=32, verbose_name=u'联系人姓名')
    business_time = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'营业时间')
    business_tel = models.CharField(max_length=16, blank=True, null=True, verbose_name=u'营业电话')
    tel = models.CharField(max_length=16, blank=True, null=True, verbose_name=u'固定电话')
    phone = models.CharField(max_length=16, blank=True, null=True, verbose_name=u'移动电话')
    site = models.CharField(max_length=255, blank=True, verbose_name=u'官方网站')
    content = models.TextField(blank=True, null=True, verbose_name=u'简介')
    map_point = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'地图坐标')
    weibo = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'微博')
    qq = models.CharField(max_length=12, blank=True, null=True, verbose_name=u'QQ')
    email = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'电子邮件')
    weixin = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'微信号')
    enable = models.SmallIntegerField(choices=enable_type, max_length=1, default=0, verbose_name=u'是否已审核')
    join_date = models.DateTimeField(default=timezone.now, verbose_name=u'加入时间')
    home_banner = models.CharField(max_length=255, blank=True, null=True, verbose_name=u'主页banner图片')
    banner_mask = models.CharField(max_length=255, blank=True, null=True, verbose_name=u'主页banner mask图片')
    has_auth = models.BooleanField(default=0, verbose_name=u'是否已认证')
    certificates = models.CharField(max_length=255, blank=True, null=True, verbose_name=u'审核不通过的原因')  #证书路径
    user = models.ForeignKey(MtUser, verbose_name=u'用户')
    score = models.IntegerField(default=0, verbose_name=u'最近7天权重')

    def __init__(self, *args, **kwargs):
        super(Supplier, self).__init__(*args, **kwargs)

        self.old_state = self.enable

    def __unicode__(self):
        return self.company

    class Meta:
        verbose_name_plural = u'服务商列表'
        db_table = 'mt_supplier'

    #保存
    def save(self, *args, **kwargs):
        #保存修改后
        super(Supplier, self).save(*args, **kwargs)

        if self.old_state == 0 or self.old_state == -1:
            if self.enable == 1:
                #修改用户组
                group_filter_data = Group.objects.filter(id=3)
                if group_filter_data.exists():
                    self.user.group = group_filter_data[0]
                    self.user.save()

                #审核后发送通知
                NoticeApi.send(
                    type='system',
                    title=u'您的服务商申请已经审核通过。',
                    content=u'<p>hi～您的婚礼服务商申请已经被审核通过。<br />马上去你的商家主页完善自己的信息吧！<br />'
                            u'<br />你的商家主页：<br /><a target="_blank" href="http://www.lovewith.me/su/%s/">'
                            u'http://www.lovewith.me/su/%s/</a>' % (self.user.id, self.user.id),
                    receiver=self.user
                )

                #发送短信通知
                sms_message = u'%s，您的婚礼服务商申请已经被审核通过，请尽快登录婚礼时光网站完善您的商家资料，自主' \
                              u'开通预约等功能。' % self.company
                DxwSmsApi().send(self.phone, sms_message)

        #修改状态为审核不通过
        if self.enable == -1:
            #审核后发送通知
            NoticeApi.send(
                type='system',
                title=u'您的服务商申请未通过审核。',
                content=u'<p>hi～您的婚礼服务商申请未通过审核，不通过理由原因：%s。<br /> 请&nbsp;'
                        u'<a href="/su/join/" target="_blank">重新提交资料</a></p>' % self.certificates,
                receiver=self.user
            )

            #发送短信通知
            sms_message = u'%s，您的婚礼服务商申请未通过审核，原因：%s' % (self.company, self.certificates)
            DxwSmsApi().send(self.phone, sms_message)

            #初次审核不通过时删除用户提交的资料
            if self.old_state == 0:
                super(Supplier, self).delete(*args, **kwargs)

            #修改用户组
            group_filter_data = Group.objects.filter(id=1)
            if group_filter_data.exists():
                self.user.group = group_filter_data[0]
                self.user.save()


#服务商列服务类型
class SupplierServiceType(models.Model):
    supplier = models.ForeignKey(Supplier, related_name="ServiceType", verbose_name=u'服务商')
    service = models.ForeignKey(ServiceCate, verbose_name=u'服务类型')
    user = models.ForeignKey(MtUser, related_name='ServiceTypeUser', verbose_name=u'用户')

    def __unicode__(self):
        return self.supplier.company

    class Meta:
        verbose_name_plural = u'服务商列服务类型'
        db_table = 'mt_supplier_service_type'


#服务商服务范围
class SupplierServiceArea(models.Model):
    city = models.ForeignKey(City, verbose_name=u'服务城市')
    supplier = models.ForeignKey(Supplier, related_name="ServiceArea", verbose_name=u'服务商')
    user = models.ForeignKey(MtUser, related_name='ServiceAreaUser', verbose_name=u'用户')

    def __unicode__(self):
        return self.city.name

    class Meta:
        verbose_name_plural = u'服务商列服务类型'
        db_table = 'mt_supplier_service_area'


#预约记录
class ReserveRecords(models.Model):
    phone = models.CharField(blank=True, null=True, max_length=13, verbose_name=u'电话')
    city = models.CharField(max_length=32, blank=True, null=True, verbose_name=u'城市')
    expected_date = models.CharField(max_length=30, verbose_name=u'期望服务时间')
    supplier = models.ForeignKey(Supplier, verbose_name=u'服务商')
    content = models.TextField(blank=True, verbose_name=u'备注')
    user = models.ForeignKey(MtUser, verbose_name=u'预约用户')

    def __unicode__(self):
        return self.subject

    class Meta:
        verbose_name_plural = u'预定订单'
        db_table = 'mt_supplier_reserve_records'
